﻿Public Class GestioneTurni

    Structure sTurni
        Dim progr As String
        Dim tipologia As String
        Dim data As String
        Dim medico As String
    End Structure

    Dim lturni As SortedList
    Dim turno As sTurni = New sTurni
    Dim indicerigaverde As Integer

#Region "Gestione ListView"

    Sub InizializzazionelstTurni(ByVal tipo As String)

        lstTurni.Enabled = True
        lstTurni.Columns.Clear()
        lstTurni.View = View.Details
        lstTurni.AllowColumnReorder = True
        lstTurni.LabelEdit = False
        lstTurni.FullRowSelect = True
        lstTurni.GridLines = True
        lstTurni.Sorting = SortOrder.None
        lstTurni.MultiSelect = False
        lstTurni.Columns.Add("Progr", 0, HorizontalAlignment.Left)
        lstTurni.Columns.Add("Tipologia", 60, HorizontalAlignment.Left)
        lstTurni.Columns.Add("Data", 60, HorizontalAlignment.Left)
        lstTurni.Columns.Add("Medico", 60, HorizontalAlignment.Left)
        lstTurni.Items.Clear()
    End Sub

    Sub AggiornaLVTurni()

        lstTurni.Items.Clear()

        Dim lstItemR As Windows.Forms.ListViewItem = Nothing
        lstTurni.BeginUpdate()
        For Indice As Int32 = 0 To lturni.Count - 1

            turno = CType(lturni.GetByIndex(Indice), sTurni)

            lstItemR = lstTurni.Items.Add(Indice)
            lstItemR.SubItems.Add(turno.tipologia)
            lstItemR.SubItems.Add(turno.data)
            lstItemR.SubItems.Add(turno.medico)

        Next
        lstTurni.EndUpdate()

    End Sub
    Sub RiorganizzaLVTurni()
        Dim lsApp As SortedList = Nothing
        Dim lcampi As sTurni = Nothing
        Dim CntRiga As Integer = 1
        lsApp = lturni.Clone

        Dim en As System.Collections.IEnumerator = lsApp.Keys.GetEnumerator()
        lturni.Clear()
        Do While en.MoveNext
            lcampi = CType(lsApp(en.Current), sTurni)
            lcampi.Progr = CntRiga
            lturni.Add(lcampi.progr, lcampi)
            CntRiga += 1
        Loop
        lsApp.Clear()
    End Sub

    'Carica i movimenti dal Database e mettendoli in struttura
    Sub RiempiLVTurni()

        Dim dtTurni As New DataTable
        dtTurni.Clear()
        'QUI BISOGNA FARE LA QUERY  

        Dim lstItemR As Windows.Forms.ListViewItem = Nothing
        lturni.Clear()
        If Not dtTurni Is Nothing Then
            For i As Integer = 0 To dtTurni.Rows.Count - 1
                turno = New sTurni
                turno.progr = i + 1
                turno.tipologia = dtTurni.Rows(i)("") 'nome dei campi del db
                turno.data = dtTurni.Rows(i)("")
                turno.medico = dtTurni.Rows(i)("")
                lturni.Add(turno.progr, turno)

            Next i
        End If

    End Sub

    'Partendo dalla struttura carica nella tabella i movimenti
    Sub CaricaLVTurni()
        lstTurni.BeginUpdate()
        lstTurni.Items.Clear()
        Dim lstItemCorpo As Windows.Forms.ListViewItem = Nothing

        For Indice As Int32 = 1 To lturni.Count

            turno = CType(lturni(Indice), sTurni)
            lstItemCorpo = lstTurni.Items.Add(Indice)
            lstItemCorpo.SubItems.Add(turno.tipologia)
            lstItemCorpo.SubItems.Add(turno.data)
            lstItemCorpo.SubItems.Add(turno.medico)

        Next
        lstTurni.EndUpdate()

    End Sub

    Private Sub lstturni_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstTurni.DoubleClick
        'TODO: ModificaRigaturni(lstTurni.SelectedIndices(0))
        indicerigaverde = lstTurni.SelectedIndices(0)  'indice della riga selezionata
    End Sub

#End Region

End Class
